package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Classes;
import db.dao.ClassesDAO;
import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Major;
import db.dao.MajorDAO;
import db.dao.Student;
import db.dao.StudentDAO;
import db.dao.Users;
import db.dao.UsersDAO;
/*添加学生*/
public class AddStudentAction extends ActionSupport{
	private String sid="";	//学生编号
	private String sname="";	//学生姓名
	private String ssex="";	//学生性别
	private String siden="";	//学生身份证号
	private Short slen;	//学制
	private String sclass="";	//所属班级
	private String smajor1="";	//所属专业
	private String sdept1="";	//所属学院
	
	public AddStudentAction() {
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getSsex() {
		return ssex;
	}

	public void setSsex(String ssex) {
		this.ssex = ssex;
	}

	public String getSiden() {
		return siden;
	}

	public void setSiden(String siden) {
		this.siden = siden;
	}

	public Short getSlen() {
		return slen;
	}

	public void setSlen(Short slen) {
		this.slen = slen;
	}

	public String getSclass() {
		return sclass;
	}

	public void setSclass(String sclass) {
		this.sclass = sclass;
	}

	public String getSmajor1() {
		return smajor1;
	}

	public void setSmajor1(String smajor1) {
		this.smajor1 = smajor1;
	}

	public String getSdept1() {
		return sdept1;
	}

	public void setSdept1(String sdept1) {
		this.sdept1 = sdept1;
	}
	//更新用户表
	public void updateUsers() {
		UsersDAO uDao = new UsersDAO();
		Users users = new Users();
		//设置参数
		users.setUsername(sid);
		users.setUserpwd(sid);
		users.setUsertype("学生");
		//保存
		uDao.save(users);
	}
	
	public String execute() throws IOException{
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//查找所属班级
		ClassesDAO cDao = new ClassesDAO();
		Classes classes = cDao.findById(sclass);
		//查找所属专业
		MajorDAO mDao = new MajorDAO();
		Major major = mDao.findById(smajor1);
		//查找所属学院
		DeptDAO dDao = new DeptDAO();
		Dept dept = dDao.findById(sdept1);
		//设置学生参数
		StudentDAO sDao = new StudentDAO();
		Student student = new Student();
		//以下参数可为空，因此进行判空
		if (classes!=null) {
			student.setClasses(classes);
		}
		if (dept!=null) {
			student.setDept(dept);
		}
		if (major!=null) {
			student.setMajor(major);
		}
		if (slen!=null) {
			student.setStuedulength(slen);
		}
		if (!ssex.equals("")) {
			student.setStusex(ssex);
		}
		student.setStuid(sid);
		student.setStuidentify(siden);
		student.setStuname(sname);
		//保存
		result = sDao.save(student);
		if (result.equals("success")) {
			//成功弹窗提示，更新用户表，更新所属班级学生人数
			updateUsers();
			if (classes!=null) {
				classes.setStunumbers((byte) (classes.getStunumbers()+1));
				cDao.save(classes);
			}
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
